<template>
  <div id="UserEntryDialog">
    <el-row>
      <el-col :span="4" class="img_logo" :offset="3">
        <p><img src="../../../src/assets/img/home/logo.png" height="100%" width="100%"></p>
      </el-col>
      <el-col :span="17">
        <h1 class="h1_title">{{contentTitle1}}</h1>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="18" :offset="3">
        <el-form :model="userForm" ref="userForm" label-width="0px">
          <el-form-item prop="accountNumber">
            <el-input v-model="userForm.accountNumber" placeholder="请输入手机号/邮箱" class="input"></el-input>
          </el-form-item>
          <el-form-item prop="passWords">
            <el-input type="password" v-model="userForm.passWords" placeholder="请输入密码"
                      @keyup.enter.native="submitForm"></el-input>
          </el-form-item>
          <el-form-item prop="verifyCode" v-if="showCode===true">
            <el-row>
              <el-col :span="15">
                <el-input v-model="userForm.verifyCode" auto-complete="off" placeholder="请输入验证码"
                          @keyup.enter.native="submitForm"></el-input>
              </el-col>
              <el-col :span="9" class="verifyImg">
                <img :src="this.img"/>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <p style="text-align:center;">
              <el-button type="primary" @click="submitForm()" class="btn_userentry">登录</el-button>
            </p>
          </el-form-item>

        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="10" :offset="9">
        <div class="grid-content bg-purple">
          <p claas="footerLink">
            <router-link to="/UserRegister" class="register"><span>新用户 | </span></router-link>
            <router-link to="/ForgetPassword" class="fotgetPw"><span>忘记密码？</span></router-link>
          </p>
        </div>
      </el-col>
    </el-row>
    <el-dialog title="提示" :visible.sync="dialogVisible"
               size="tiny">
      <el-row>
        <el-col :span="18" :offset="3">
          <div class="grid-content bg-purple-dark">
            <UserPrompt></UserPrompt>
          </div>
        </el-col>
      </el-row>
      </br>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple-dark" style="text-align:center;">
            <el-button type="primary" @click="routerLink">取 消
            </el-button>
          </div>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
  import UserPrompt from '@/components/userLogin/UserPrompt'
  import { postDataToServer } from '../../../src/assets/js/post.js'
  import { getUserType } from '../../../src/assets/js/common.js'
  import { optionCookie } from '../../../src/assets/js/cookie.js'

  export default {
    props: ['type'],//type登录成功后跳转的页面
    data () {
      return {
        contentTitle1: '绵阳人才市场',
        userForm: {
          accountNumber: '',
          passWords: '',
          verifyCode: '',
        },
        dialogVisible: false,
        showCode: false,
        imgUrl: '',
        imgData: 'data:image/png;base64,',
        img: '',
        highTalent: 0,//0不是专家，1是国内专家，2国外专家
      }
    },
    created () {

    },
    components: {
      UserPrompt,
    },
    methods: {
      open (type, message) {
        this.$message({
          type: type,
          message: message
        })
      },
      routerLink () {
        this.$router.push({path: '/'})
      },
      getPrompt () {
        let _this = this
        let pramas = new Object()
        let m = 0
        pramas.subsys_id = sysData[0].sysid
        postDataToServer('ACCOUT_ROLES', pramas).then((data) => {
          if (data.status === 0) {
            console.log('hhhhhhh')
            _this.m = data.data_list.length
            if (_this.m === 0) {
              _this.dialogVisible = true
            } else {
              _this.$router.push({path: '/'})
            }

          }
        })
      },
      submitForm () {
        var jsSHA = require('jssha')
        var _this = this
        var userParam1 = new Object()
        userParam1.user_account = _this.userForm.accountNumber
        postDataToServer('CAPCHA_CHECK', userParam1).then(function (data) {
          if (data.status == 2 || data.status == 3 || data.status == 23) {
            var userParam3 = new Object()
            userParam3.bind_account = _this.userForm.accountNumber
            userParam3.account_type = '2'
            userParam3.verify_type = '4'
            postDataToServer('CAPCHA_GET', userParam3).then(function (data) {
              _this.imgUrl = data.data_list
              _this.img = _this.imgData + _this.imgUrl
              _this.showCode = true
              if (data.status == 0) {
                var userParam4 = new Object()
                var shaObj = new jsSHA('SHA-256', 'TEXT')
                shaObj.update(_this.userForm.passWords)
                var hash = shaObj.getHash('HEX')
                userParam4.user_password = hash
                userParam4.user_account = _this.userForm.accountNumber
                userParam4.verify_code = _this.userForm.verifyCode
                postDataToServer('USER_LOGIN', userParam4).then(function (data) {
                  if (data.status === 0) {
                    let cookie = new Object()
                    cookie.name = 'access_token'
                    cookie.value = data.access_token
                    optionCookie('set', cookie)
                    _this.$emit('entryReturn', true)
                    _this.open('success', '登录成功！')
                    _this.getPrompt()
                  } else {
                    _this.$emit('entryReturn', false)
                    _this.open('warning', data.message)
                  }
                })
              } else {
                _this.open('warning', data.message)
              }
            })
          } else if (data.status == 0) {
            var userParam2 = new Object()
            var shaObj = new jsSHA('SHA-256', 'TEXT')
            shaObj.update(_this.userForm.passWords)
            var hash = shaObj.getHash('HEX')
            userParam2.user_password = hash
            userParam2.user_account = _this.userForm.accountNumber
            postDataToServer('USER_LOGIN', userParam2).then(function (data) {
              if (data.status == 0) {
                let cookie = new Object()
                cookie.name = 'access_token'
                cookie.value = data.access_token
                optionCookie('set', cookie)
                _this.$emit('entryReturn', true)
                _this.open('success', '登录成功！')
                _this.getPrompt()
              } else {
                _this.$emit('entryReturn', false)
                _this.open('warning', data.message)
              }
            })
          } else {
            _this.open('warning', data.message)
          }
        })
      }
    }
  }
</script>
<style scoped>
  .logoText h1 {
    text-align: center;
    color: white;
    font-size: 2em;
  }

  .img_logo p {
    text-align: center;
    line-height: 2em;
  }

  .h1_title {
    height: 1.2em;
    line-height: 1.8em;
    font-size: 1.3em;
    margin-left: 10px;
  }

  .btn_userentry {
    width: 50%;
  }

  .register:link, .register:active, .register:visited, .register:hover {
    color: #000;
    text-decoration: none;
  }

  .fotgetPw:link, .fotgetPw:active, .fotgetPw:visited, .fotgetPw:hover {
    color: #000;
    text-decoration: none;
  }

  .verifyImg {
    margin-top: 2%;
  }
</style>
